import nci60.data.protein.*
import nci60.data.maxquant.*

def currentProject = Project.findById(1)
def experiments = Experiment.findAllByCurrentProject(currentProject)

println currentProject
println experiments

def experimentsIndex = [:]
def proteinIndex

boolean firstLine = true

def startPoint = 0


def file = new File("/home/auerf/data/bsp/NCI60-Full/Original-full-proteome.txt")

println "INSERT INTO protein_experiment (current_project_id, experiment_id, intensity, protein_id) VALUES"

def counter = 0;

file.eachWithIndex(){line, lineNr->
    def columns = line.split("\t")
    if(firstLine){
        experiments.each(){exp->
            columns.eachWithIndex(){col,i->
                if(col==exp.name){
                    experimentsIndex.put(exp.name, i)
                }else if(col=="Accession"){
                    proteinIndex = i
                }
            }
        }
        
        firstLine=false
    }else{
        if(lineNr>startPoint){
            def protein = Protein.findByMainId(columns[proteinIndex])
//            protein.identified = true
//            protein.save(flush:true)
            experiments.each(){exp->
                counter++
                if(counter%1000==0) {
                    println "("+currentProject.id+", "+exp.id+", "+columns[experimentsIndex[exp.name]].replace("NaN","null")+", "+protein.id+");"
                    println "INSERT INTO protein_experiment (current_project_id, experiment_id, intensity, protein_id) VALUES"
                }else{
                    println "("+currentProject.id+", "+exp.id+", "+columns[experimentsIndex[exp.name]].replace("NaN","null")+", "+protein.id+"),"
                }
            }
        }
//        if(lineNr>startPoint){
//            println lineNr
//            def protein = Protein.findByMainId(columns[proteinIndex])
//            println "\t"+protein.mainId
//            protein.identified = true
//            def proteinExperiment
//            experiments.each(){exp->
////                println "\t\t"+exp.name
//
//                proteinExperiment = new ProteinExperiment([experiment:exp, currentProject:currentProject, protein:protein])
//
//                if(columns[experimentsIndex[exp.name]]!="NaN"){
//                    proteinExperiment.intensity = columns[experimentsIndex[exp.name]].toDouble()
//                }
//
//                if(!proteinExperiment.save(flush:true)){
//                    proteinExperiment.errors.each {
//                        println it
//                    }
//                }
//
////                println "\t\t"+ proteinExperiment
//                protein.experiments.add(proteinExperiment)
//            }
//            protein.save(flush:true)
//        }
    }
    
}